// Thread.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Thread.h"


	// define a threadable object
	class ThreadableObject : public IRunnable {
	public:
		ThreadableObject() {
			_continue = true;	
		}
		virtual unsigned long run() {
			while(_continue) {
				// run this thread procedure
				Sleep(5000);
			}
			return 0;
		}
		virtual void stop() {
			_continue = false;
		}
	protected:
		bool _continue;		
	};



int _tmain(int argc, _TCHAR* argv[]) {
	// example of usage

	ThreadableObject *obj=0;
	Thread *thread=0;

	try {
		// create the threadable object first
		ThreadableObject *obj = new ThreadableObject();

		// create and start the thread the thread
		Thread *thread = new Thread(obj);
		thread->start();

		// see if the thread exits in 10 seconds
		thread->join(10000);

		if(thread->isAlive()) {
		    // stop it and join until the thread exits
			thread->stop();
			thread->join();
		}

		printf("Ok.\n");

	} 
	catch (ThreadException &e)
	{
		printf(e.Message.c_str());	
	}

	delete obj;
	delete thread;
	return 0;
}

